package com.zhidisoft.system.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.zhidisoft.base.dao.impl.BaseDaoImpl;
import com.zhidisoft.system.dao.IUserDao;
import com.zhidisoft.system.entity.User;

/**
 * 用户数据库操作实现
 * @author Lu Jianliang
 *
 */
@Repository
public class UserDaoImpl extends BaseDaoImpl<User, String> implements IUserDao{

	public UserDaoImpl(){
		super(User.class);
	}

	@Override
	public User findByUsername(String username) {
		Query query = session().createQuery("from User where username=?");
		query.setParameter(0, username);
		return (User)query.uniqueResult();
	}

	@Override
	public List<User> findByPage(Long pageNumber, Long pageSize) {
		String hql = "from User u left join fetch u.createUser left join fetch u.updateUser left join fetch u.roles order by u.sortNum";
		return this.findByPage(pageNumber.intValue(), pageSize.intValue(), hql, new Object[]{});
	}

	@Override
	public boolean delete(String[] ids) {
		String hql = "delete from User where id in (:ids)";
		Query query = session().createQuery(hql).setParameterList("ids", ids);
		int rows = query.executeUpdate();
		if(rows>0){
			return true;
		}
		return false;
	}
	
}
